<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <!-- 插件编辑器核心 -->
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <!--如果需要图片上传-->
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/common.js"></script>

<script>
    $(function () {

        var authorId = JSON.parse(sessionStorage.getItem("user")).id;
        var url;
        //编辑器配置
        var Edr = new Eleditor({
            el: '#contentEditor',//在页面那个位置显示
            upload:{
                server: '/images',//图片上传你的路径
            }
        });
        //更换封面按钮绑定点击事件
        $("#changeBtn").click(function () {
            //触发文件上传
            $("#fileinputBtn").click();
        });
        //给文件上传的值改变绑定事件提交表单
        $("#fileinputBtn").change(function () {
            //如果有值才进行提交
            if ($(this).val()) {
                //图片上传提交表单
                $("#fileForm").ajaxSubmit(function (data) {
                    console.log(data);
                    if (data.status==1) {
                        //把图片回显到背景中
                        $("#coverUrl").attr("src",data.url);
                        url = data.url;
                    }
                });
            }
        });
        //给出行时间绑定点击事件(使用时间插件)
        $("#travelTime").click(function () {
            WdatePicker({
                readOnly:true,
                maxDate:new Date()
            });
        });
        //查询出所有的旅游地区
        $.get('/regions',function (data) {
            //将旅游地区拼接进表单的旅游地区中
            var temp;//拼接option
            $.each(data,function (index,ele) {
                temp+="<option value='"+ele.id+"'>"+ele.name+"</option>"
            })
            $(".placeSelect").html(temp);//设置进下拉框中

            //获取游记id,对游记编辑进行回显查询
            var params = getParams();//获取拼接在资源名后的值
            console.log(params);
            if (params.id) {//如果有值,发送ajax请求获取需要编辑的id游记的数据
                $.get("/travels/"+params.id,function (data) {
                    url = data.coverUrl;
                    //修改表单提交方式
                    $("#detailForm").attr("action","/travels/"+data.id);
                    //进行数据渲染
                    $(".travels").renderValues(data);
                })
            }
        });

        $("#detailForm").ajaxForm({
            beforeSubmit:function (arr) {//提交表单之前,设置提交信息
                //实例化文本编辑内容
                var content = Edr.getContent();
                arr.push({name:'title',value:$("#title").val()});
                arr.push({name:'coverUrl',value:url});
                arr.push({name:'travelcontent.content',value:content});
                arr.push({name:'author.id',value:authorId});
            },
            success:function (data) {//表单提交成功后执行的函数
                if(data.success){
                    window.location.href = "/mine/travels.html";//我的游记
                }
            }
        });

        //点击保存提交表单
        $("#save").click(function () {
            $("#detailForm input[name='state']").val(0);
            //提交表单
            $("#detailForm").submit();
        });

        //点击发布提交表单
        $("#release").click(function () {
            $("#detailForm input[name='state']").val(1);
            $("#detailForm").submit();
        });
    })
</script>
</head>

<body>
<!--文件上传表单-->
<form id="fileForm" action="/images" method="post" enctype="multipart/form-data">
    <input id="fileinputBtn" type="file" name="file" style="display: none" >
</form>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" render-src="coverUrl" width="100%" height="200px" />
       
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
       

    </div>
    <div class="title">
        <input class="form-control" id="title"  placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelcontent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" action="/travels">
            <input type="hidden" name="_method" value="put"/>
            <label>出行时间:</label>
            <input type="text" name="travelTime" id="travelTime" class="form-control" render-value="travelTime"/>
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days"/>
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends"/>

            <label>旅游地点:</label>
            <select class="form-control placeSelect" name="place.id" render-value="place.id">
            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox"  name="isPublic" render-attr="checked=isPublic"
                          >是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>


</body>

</html>